package com.xiaoai.io.bio6_zport;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;

/**
 * @Author
 * @Date 2021-09-06 00:47
 */
public class serverThreadReader extends Thread{
    private Socket socket;
    public serverThreadReader(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        Server.sockets.add(socket);
        try {
            InputStream is = socket.getInputStream();
            BufferedReader bis = new BufferedReader(new InputStreamReader(is));
            String msg;
            while ((msg = bis.readLine()) != null ){
                System.out.println("服务器接收到["+socket.toString()+"]：" + msg);
                // 消息转发
                for (Socket sk : Server.sockets) {
                    PrintStream ps = new PrintStream(sk.getOutputStream());
                    ps.println(msg);
                    ps.flush();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        super.run();
    }
}
